package parse

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"log"
	"regexp"
	"spider/common"
	"spider/fetcher"
	"spider/itemSaver"
	"spider/models"
	"spider/models/meituan/meituanMeishiPc"
	"strconv"
	"strings"
)

//pc美团美食列表页内容的解析,构建下级请求Request集合

func ParseMeituanList(str []byte, dfp models.DataFromParent) (parseRequestResult models.ParseRequestResult, err error) {
	var (
		request  models.Request
		deatilRequests []models.Request
		allRequesus []models.Request
		header   models.Header
		pn       int
		page 	int
		res      meituanMeishiPc.ResArr
	)
	//匹配要往详情页的数据
	expr := `"pn":(\d+),"poiLists":({.*}]})`
	rgp, err := regexp.Compile(expr)

	if err != nil {
		return
	}

	pregRes := rgp.FindAllStringSubmatch(string(str), -1)
	lenRes := len(pregRes)
	if lenRes < 1 {
		err = fmt.Errorf("FindAllStringSubmatch content=%s", str)
		return
	}

	pn, err = strconv.Atoi(pregRes[0][1])
	if err != nil {
		err = fmt.Errorf("Atoi err=%s", err)
		return
	}

	jsonstr := pregRes[0][2]
	err = json.Unmarshal([]byte(jsonstr), &res)
	if (err != nil) {
		return
	}
	size := len(res.PoiInfos)
	var arr = make([]int, 0, size)
	//构建到详情页的请求
	header = models.Header{
		"Content-Type": "application/json",
		"Cookie":       common.G_config.Cookie,
	}
	header = common.MergreHeader(header, common.CommonHeader)
	exitsNum:=0;
	for _, v := range res.PoiInfos {
		if itemSaver.ExistsByPoiId(v.PoiId) {
			//log.Printf("Exist0 poiId=%d\n", v.PoiId)
			exitsNum++
			continue
		}
		arr = append(arr, v.PoiId)
		tmpHeader := header
		referer := fmt.Sprintf("https://www.meituan.com/meishi/%d/", v.PoiId)
		tmpHeader["Referer"] = referer

		var dataToSon = make(models.DataFromParent)
		//传递给下个请求时
		dataToSon["poiId"] = v.PoiId
		//详情页请求的
		request = models.Request{
			Url:            "https://www.meituan.com/meishi/api/poi/getFoodSafetyDetail",
			Header:         tmpHeader,
			PostData:       fmt.Sprintf(`{"poiId":"%d"}`, v.PoiId),
			ReqFunc:        fetcher.MeiTuanPost,
			Parser:         ParsePopOne,
			ParserName:     "ParsePopOne",
			DataFromParent: dataToSon,
		}
		deatilRequests = append(deatilRequests, request)
	}
	if exitsNum>0{
		fmt.Printf("exitsNum=====%d\n",exitsNum)
	}

	//构建下一页的请求

	var (
		listHeader models.Header
		nextUrl    string
		cate       string
	)
	listHeader = make(models.Header)

	listHeader["Referer"] = "https://bj.meituan.com/meishi/"

	listHeader = common.MergreHeader(listHeader, common.CommonHeader)

	var CateRequests []models.Request
	//说明是首页,在首页需要分析类别
	if dfp["cate"] == "" && dfp["page"] == 1 {
		//首页的下一页
		nextUrl = fmt.Sprintf("https://bj.meituan.com/meishi/pn%d/", pn+1)

		reader := bytes.NewReader(str)
		doc, err := goquery.NewDocumentFromReader(reader)
		if err == nil {
			doc.Find(".condition .cont").First().Find("ul li").Each(func(i int, selection *goquery.Selection) {
				href, exists := selection.Find("a").First().Attr("href")
				log.Printf("href=%s\n",href);
				if (exists) {
					cateHref := strings.Trim(href, "/")
					cateArr := strings.Split(cateHref, "/");
					cate = cateArr[len(cateArr)-1]
					cateRequest := models.Request{
						Url:            href,
						Header:         header,
						ReqFunc:        fetcher.FetchGet,
						Parser:         ParseMeituanList,
						ParserName:     "ParseMeituanList",
						DataFromParent: models.DataFromParent{"cate": cate, "page": 1},
					}
					CateRequests = append(CateRequests, cateRequest)
				}
			})
		}

	} else {
		//全部的非首页 或者类别页及其下一页
		if page, ok := dfp["page"].(int); ok {
			//全部页的下页
			if dfp["cate"] == "" {
				nextUrl = fmt.Sprintf("https://bj.meituan.com/meishi/pn%d/", page+1)
			} else {
				//带分类页的下一页
				nextUrl = fmt.Sprintf("https://bj.meituan.com/meishi/%s/pn%d/", dfp["cate"], page+1)
			}
		}else{
			panic("page lost")
		}
	}
	log.Printf("cates=%d;add url=%s,dfp=>%+v\n",len(CateRequests),nextUrl,dfp)


	reqNext := models.Request{
		Url:            nextUrl,
		Header:         listHeader,
		ReqFunc:        fetcher.FetchGet,
		Parser:         ParseMeituanList,
		ParserName:     "ParseMeituanList",
		DataFromParent: models.DataFromParent{"page": page + 1, "cate": cate},
	}

	allRequesus=append(deatilRequests,reqNext)  //本页爬取的详情页，和下一页
	allRequesus=append(allRequesus,CateRequests...) //分类页


	parseRequestResult = models.ParseRequestResult{
		Items:    arr,
		Requests: allRequesus,
	}
	return
}

//pc 解析单条数据 食品安全档案
func ParsePopOne(str []byte, dfp models.DataFromParent) (parseRequestResult models.ParseRequestResult, err error) {
	var (
		res   meituanMeishiPc.LicenseResArr
		items []meituanMeishiPc.LicenseResData
		poiId int
	)
	err = json.Unmarshal(str, &res)
	if err != nil {
		return
	}
	switch v := dfp["poiId"].(type) {
	case int:
		poiId = v
	}

	res.Data.PoiId = poiId
	items = append(items, res.Data)

	var requests []models.Request
	parseRequestResult = models.ParseRequestResult{
		Items:    items,
		Requests: requests, //没有下级的请求了
	}
	return
}
